//
//  CircleView.m
//  SinaWebo
//
//  Created by wwj on 16/6/27.
//  Copyright © 2016年 亦心. All rights reserved.
//

#import "CircleView.h"
#import "CircleViewCell.h"

@interface CircleView ()<UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) UICollectionViewFlowLayout *flowLayout;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pageWith;
@property (strong, nonatomic) NSTimer *timer;
@end

@implementation CircleView
static NSString *const ID = @"CircleViewCell";
static const NSInteger kMultiply = 10000;

+ (instancetype)circleView{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([CircleView class]) owner:nil options:nil] lastObject];
}

- (void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    _pageControl.numberOfPages = _dataArray.count;
    _pageWith.constant = _dataArray.count * 15.0;
   
    _collectionView.contentInset = UIEdgeInsetsZero;
    _flowLayout.minimumLineSpacing = 0.0;
    _flowLayout.minimumInteritemSpacing = 0.0;
    
//    NSInteger row = _dataArray.count * kMultiply * 0.5;
//    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    
    [self.collectionView reloadData];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    _flowLayout.itemSize = self.size;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout = flowLayout;
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView.collectionViewLayout = flowLayout;
    _collectionView.pagingEnabled = YES;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CircleViewCell class]) bundle:nil] forCellWithReuseIdentifier:ID];
        
    [self addTimer];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataArray.count * kMultiply;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CircleViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.imageURL = _dataArray[indexPath.row % _dataArray.count];
    return cell;
}

- (void)onStartTimer{
    [self.collectionView setContentOffset:CGPointMake(self.collectionView.contentOffset.x + self.width, 0) animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (!_dataArray.count) return;
    self.pageControl.currentPage = ((NSInteger)((scrollView.contentOffset.x + self.width) / self.width) - 1) % self.dataArray.count;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self remoteTimer];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self addTimer];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([_delegate respondsToSelector:@selector(circleView:didSelectItemAtIndex:)]) {
        [_delegate circleView:self didSelectItemAtIndex:indexPath.row % _dataArray.count];
    }
}


- (void)addTimer{
    _timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(onStartTimer) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}

- (void)remoteTimer{
    [_timer invalidate];
    _timer = nil;
}


@end
